package com.chinasofti.service.impl;

import com.chinasofti.dao.UserDao;
import com.chinasofti.dao.impl.UserDaoImpl;
import com.chinasofti.domain.User;
import com.chinasofti.service.UserService;
import com.chinasofti.util.JDBCUtils;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    @Override
    public void batchAdd(List<User> users) {
        //获取数据库连接
        Connection connection = JDBCUtils.getConnection();

        try {
            //开启事务
            connection.setAutoCommit(false);

            for (User user:users){
                //创建UUID 并把 - 替换
                String uid = UUID.randomUUID().toString().replace("-","").toUpperCase();
                //给user设置uid
                user.setUid(uid);
                //给员工编号
                user.setUcode(uid);

                //保存
                userDao.save(connection,user);
            }

            //提交事务
            connection.commit();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}